import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
const routes = [
    {
        path: '/',
        component: Layout,
        redirect: '/',
        meta: { title: '首页', icon: 'alipay-circle-outlined' },
        children: [
            {
                path: '/',
                name: 'Home',
                component: () => import('@/views/home/index.vue'),
                meta: { title: '首页', icon: 'alipay-circle-outlined' }
            }
        ]
    },
    {
        path: '/user',
        component: Layout,
        redirect: '/user',
        children: [
            {
                path: '/user',
                name: 'User',
                component: () => import('@/views/user/index.vue'),
                meta: { title: '用户管理', icon: 'appstore-filled' }
            },
            {
                path: '/user1',
                name: 'User1',
                component: () => import('@/views/user/index1.vue'),
                meta: { title: '用户管理1', icon: 'appstore-filled' }
            }
        ]
    },
    {
        path: '/goods',
        component: Layout,
        redirect: '/goods',
        children: [
            {
                path: '/goods',
                name: 'Goods',
                component: () => import('@/views/goods/index.vue'),
                meta: { title: '商品管理', icon: 'appstore-filled' }
            }
        ]
    },
    {
        path: '/category',
        component: Layout,
        redirect: '/category',
        children: [
            {
                path: '/category',
                name: 'Category',
                component: () => import('@/views/category/index.vue'),
                meta: { title: '分类管理', icon: 'appstore-filled' }
            }
        ]
    },
    {
        path: '/slides',
        component: Layout,
        redirect: '/slides',
        children: [
            {
                path: '/slides',
                name: 'Slides',
                component: () => import('@/views/slides/index.vue'),
                meta: { title: '轮播管理', icon: 'appstore-filled' }
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/index.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    let token = JSON.parse(sessionStorage.getItem('user'))
        ? JSON.parse(sessionStorage.getItem('user')).access_token
        : null
    // 如果你要去的不是登录页且没有token 那么将强制跳转到登录页
    if (to.path !== '/login' && !token) {
        next('/login')
    } else if (to.path === '/login' && token) {
        // 如果已经登录  不要再去登录页
        next(from.path)
    } else {
        // 如果有了那么放行
        next()
    }
})
export default router
